﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Edge.Common.Utils;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Web.AppCode;
using SortDirection=Edge.Common.SortDirection;

namespace Shop.Web
{
    public partial class Catalog : ShopBasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProductGroupFilter filter = new ProductGroupFilter();
                WebParameterAttribute.SetValues(filter, Request);

                //            if (Request.QueryString.Count == 0)
                //                //TODO set up default catalogue
                //                
                //            else
                int count = Locator.GetService<IProductGroupManager>().Count(filter);
                if (count == 1)
                {
                    ProductGroup productGroup =
                        CollectionsUtils.GetElement(
                            Locator.GetService<IProductGroupManager>().Find(FetchOptions.All, filter), 0);
                    Response.Redirect("Product.aspx?id=" + productGroup.Id.ToString());
                }
                else
                {
                    FetchOptionForm optionForm = new FetchOptionForm();
                    WebParameterAttribute.SetValues(optionForm, Request);
                    FetchOptions fetchOptions = optionForm.Parse();
                    Customer customer = Locator.GetService<IContextObjects>().Get<CustomerHolder>() != null
                                            ? Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object
                                            : null;
                    fetchOptions.SortExpression = "Qty";
                    fetchOptions.SortDirection = SortDirection.Descending;
                    filter.ShowInVis = customer != null && customer.Role == CustomerRole.Admin;
                    ctrlCatalog.DataSource = Locator.GetService<IProductGroupManager>().Find(fetchOptions, filter);
                    ctrlPager.SetPager(count, optionForm);
                    BuildPath(filter);
                    string title = ltPath.Text;
                    if (optionForm.Page > 1)
                    {
                        title += " - страница " + optionForm.Page.ToString();
                    }
                    SetTitle(title);
                }
            }
        }

        private void DataBind()
        {
//            ddlBrend.DataSource = Locator.GetService<IBrandManager>().FindAll();
//            ddlBrend.DataTextField = "Name";
//            ddlBrend.DataValueField = "Id";
//            ddlBrend.DataBind();
//            ddlBrend.Items.Insert(0,new ListItem("все бренды",""));
            

        }

        private void BuildPath(ProductGroupFilter filter)
        {
            string tmp = string.Empty;
            ltPath.Text = "Каталог";
            if (filter.Category != null)
            {
                ltPath.Text = filter.Category.Name;
                lbDescription.Text = StringUtils.TextileToHtml(filter.Category.Description);
                if (filter.Brand != null)
                {
                    tmp = "<li><a href='Catalog.aspx?catId="+filter.Category.Id.ToString() +"'>" + filter.Category.Name + "</a></li>";
                    ltPath.Text = filter.Brand.Name;
                    lbDescription.Text += "<bt />"+StringUtils.TextileToHtml(filter.Brand.Description);
                }
                if (filter.Category.ParentCategory != null)
                {
                    tmp = "<li><a href='Catalog.aspx?catId=" + filter.Category.ParentCategory.Id.ToString() + "'>" + filter.Category.ParentCategory.Name + "</a></li>" + tmp;
                    if (filter.Category.ParentCategory.ParentCategory != null)
                    {
                        tmp = "<li><a href='Catalog.aspx?catId=" + filter.Category.ParentCategory.ParentCategory.Id.ToString() + "'>" + filter.Category.ParentCategory.ParentCategory.Name + "</a></li>" + tmp;
                        if (filter.Category.ParentCategory.ParentCategory.ParentCategory != null)
                        {
                            tmp = "<li><a href='Catalog.aspx?catId=" + filter.Category.ParentCategory.ParentCategory.Id.ToString() + "'>" + filter.Category.ParentCategory.ParentCategory.ParentCategory.Name + "</a></li>" + tmp;
                        }
                    }
                }
                
            }
            else if (filter.Brand != null)
            {
                ltPath.Text = filter.Brand.Name;
                lbDescription.Text = StringUtils.TextileToHtml(filter.Brand.Description);
            }
            ltPrePath.Text = tmp;
          
        }
    }
}
